<?php

/**
 *  消息控制器
 * @author zhangl   zhangl@wanthings.com
 * @copyright 成都网信天成科技有限公司, 2016, http://www.wanthings.com
 */
class MessageController extends Controller
{
    public function actionList()
    {
        // echo Message::model()->getNumByType(Message::ABNORMAL_TYPE,Yii::app()->user->getId());exit;
        // var_dump(Message::model()->addMessage(array('message_type'=>10,'type'=>1,'content'=>'欢迎使用车货匹配系统','to_user_type'=>2)));exit;
        $this->pageTitle = "消息列表";
        $filter['to_user_sid'] = Yii::app()->user->getId();
        $filter['type'] = Yii::app()->request->getQuery('type');
        $filter['page'] = Yii::app()->request->getQuery('page', 1);
        $filter['page_size'] = Yii::app()->request->getQuery('page_size', 20);
        $messages = Message::model()->listMessage($filter);
        $type = Message::$type;
        $this->render('list', array('messages' => $messages['messages'], 'pager' => $messages['pager'], 'type' => $type));
    }

    public function actionAdd()
    {
        $type = Yii::app()->request->getPost('type');
        $user_sid = Yii::app()->user->getId();
        $to_user_id = Yii::app()->request->getPost('to_user_id');
        $content = Yii::app()->request->getPost('content');
        $res = Message::model()->add($type, $to_user_id, $content, $user_sid);
    }

    public function actionNewmsg()
    {
        $result = array();
        $result['errno'] = 0;
        $newMessages = Message::model()->newMessage(Yii::app()->user->getId());
        if (!empty($newMessages)) {
            $result['result'] = $newMessages;
        } else {
            $result['result'] = array();
        }
        echo CJSON::encode($result);
    }
}